﻿using System;
using System.Collections;
using System.ComponentModel;
using EDMDesigner.Business.CSDL.Type;
using EDMDesigner.Business.CSDL.Util;
using EDMDesigner.Business.Util;

namespace EDMDesigner.Business.CSDL.Property
{
    public abstract class PropertyBase : INotifyPropertyChanged, INamedElement
    {
        private TypeBase _entityType;
        public TypeBase EntityType 
        {
            get { return _entityType; }
            internal set
            {
                if (_entityType == value)
                    return;
                if (_entityType != null)
                    GetPropertyCollection(_entityType).Remove(this);
                _entityType = value;
            }
        }
        internal void AddToType(TypeBase type)
        {
            if (type != null)
            {
                EntityType = type;
                GetPropertyCollection(type).Add(this);
            }
        }
        protected abstract Func<TypeBase, IList> GetPropertyCollection {get;}

        private string _name;
        public string Name
        {
            get { return _name; }
            set 
            { 
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private Visibility _getVisibility; 
        [DisplayName("Getter")]
        public Visibility GetVisibility
        {
            get { return _getVisibility; }
            set 
            {
                _getVisibility = value;
                OnPropertyChanged("GetVisibility");
            }
        }

        internal virtual PropertyBase Duplicate()
        {
            var value = Create();
            value.Name = Name;
            value.GetVisibility = GetVisibility;
            return value;
        }
        protected abstract PropertyBase Create();

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            return Name;
        }
    }
}
